package com.hz.sdka.sdka;

import android.app.Application;
import android.util.Log;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustConfig;
import com.adjust.sdk.LogLevel;
import com.anythink.core.api.ATSDK;
import com.facebook.FacebookSdk;
import com.hz.sdka.commona.SDKContext;
import com.hz.sdka.commona.utils.LogUtils;
import com.hz.sdka.commona.utils.SPUtils;

public class AdInitializea {

    public void init(Application application, String appid, String appkey, String umChannel, String adJustKey) {
        SDKContext.getInstance().init(application);
        ATSDK.integrationChecking(application);
        //初始化TopOn SDK之前调用此方法
        ATSDK.setChannel(umChannel);
        ATSDK.init(application, appid, appkey);
        String appToken = adJustKey;
        String environment = AdjustConfig.ENVIRONMENT_PRODUCTION;
        AdjustConfig config = new AdjustConfig(application, appToken, environment);
        config.setLogLevel(LogLevel.VERBOSE);
        config.setOnAttributionChangedListener(attribution -> {
            Log.i("AdInitializea：", attribution.toString());
            if ("Organic".equals(attribution.trackerName) || "Organic".equals(attribution.network)) {
                SPUtils.putBoolean(SDKContext.getInstance().getContext(), "install_channel", true);
            } else {
                SPUtils.putBoolean(SDKContext.getInstance().getContext(), "install_channel", false);
            }
        });
        Adjust.onCreate(config);
        new FacebookSdk();
    }


    /**
     * 判断是否为自然量用户
     *
     * @return true 自然量 不弹应用外
     */
    public boolean isOrganic() {
        try {
            return SPUtils.getBoolean(SDKContext.getInstance().getContext(), "install_channel", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public void setDebug(boolean isDebug) {
        if (isDebug) {
            ATSDK.setBiddingTestDevice("c79ef78607ac4e81");
            ATSDK.setNetworkLogDebug(true);
            LogUtils.setDebug(true);
        }
    }
}
